//
//  CommunityViewController.m
//  Pianke
//
//  Created by BetaTheta on 16/1/21.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "CommunityViewController.h"
#import "Community_TopicModel.h"
#import "Community_TopicCell.h"
#import "Community_TopicWithImgCell.h"

#import "PWNetWorking.h"
#import "UIImageView+WebCache.h"

@interface CommunityViewController ()<UITableViewDataSource, UITableViewDelegate>
// navigationBarButton
@property (nonatomic, retain) UIBarButtonItem *topicItem;
@property (nonatomic, retain) UIBarButtonItem *groupItem;
@property (nonatomic, retain) UIBarButtonItem *selfItem;
// 底部滚动视图
@property (nonatomic, retain) UIScrollView *backgroundSV;

@property (nonatomic, retain) UITableView *topicTableView;
@property (nonatomic, retain) NSMutableArray *topicDataArr;


@end

@implementation CommunityViewController
// 内存管理
- (void)dealloc
{
    [_topicItem release];
    [_groupItem release];
    [_selfItem release];
    [_backgroundSV release];
    [_topicTableView release];
    [_topicDataArr release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createNavigationItem];
    [self createBackgroundSV];
    [self dataHandler];
    [self createTableView];
    
    // 导航栏按钮 下方线view
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(268, 64, 27, 5)];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.tag = 1001;
    [self.view addSubview:lineView];

}
- (void)createNavigationItem
{
    // 导航栏按钮
    self.topicItem =  [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_huati.png"] style:UIBarButtonItemStylePlain target:self action:@selector(scrollAction:)] autorelease];
    self.groupItem =  [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_group.png"] style:UIBarButtonItemStylePlain target:self action:@selector(scrollAction:)] autorelease];
    self.selfItem =  [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_ziji.png"] style:UIBarButtonItemStylePlain target:self action:@selector(scrollAction:)] autorelease];
    /**< item颜色 */
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    // 添加右按钮
    self.navigationItem.rightBarButtonItems = @[_selfItem, _groupItem, _topicItem];
}
// 创建底部滚动视图
- (void)createBackgroundSV
{
    CGRect f =  self.view.frame;
    f.origin.y += 69;
    f.size.height -= 69;
    self.backgroundSV = [[UIScrollView alloc] initWithFrame:f];
    _backgroundSV.contentSize  = CGSizeMake(self.view.bounds.size.width * 3, 0);
    
    // 设置scrollView
    
    _backgroundSV.pagingEnabled = YES;
    _backgroundSV.showsHorizontalScrollIndicator = NO;
    _backgroundSV.showsVerticalScrollIndicator = NO;
    _backgroundSV.bounces = NO;

    [self.view addSubview:_backgroundSV];
    [_backgroundSV release];
}
// 数据处理
- (void)dataHandler
{
    NSString *topicUrl = @"http://api2.pianke.me/group/posts_hotlist";
    // auth=&client=1&deviceid=DB3A1424-3E92-41D9-8C86-10F84629D968&limit=10&sort=hot&start=0&version=3.0.6
    NSDictionary *topicBodyDic = @{@"auth": @"", @"client": @"1", @"deviceid": @"DB3A1424-3E92-41D9-8C86-10F84629D968", @"limit": @"10", @"sort":@"hot", @"start":@"0", @"version": @"3.0.6"};
    [PWNetWorking POSTWithURL:topicUrl body:topicBodyDic completion:^(id result) {
        self.topicDataArr =[Community_TopicModel modelHanderWithArray:result[@"data"][@"list"] ];
        NSLog(@"%@", result);
        [_topicTableView reloadData];
    }];
}
// 创建表视图
- (void)createTableView
{
    self.topicTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, - 69, self.view.bounds.size.width, self.view.bounds.size.height)];
    _topicTableView.delegate = self;
    _topicTableView.dataSource = self;
    [_topicTableView registerClass:[Community_TopicCell class] forCellReuseIdentifier:@"topicWithoutImg"];
    [_topicTableView registerClass:[Community_TopicWithImgCell class] forCellReuseIdentifier:@"topicWithImg"];
    [_backgroundSV addSubview:_topicTableView];
    [_topicTableView release];
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _topicDataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Community_TopicModel *tM = _topicDataArr[indexPath.row];
    if (tM.icon == 0) {
        Community_TopicCell *topicCell = [tableView dequeueReusableCellWithIdentifier:@"topicWithoutImg" forIndexPath:indexPath];
        topicCell.titleLabel.text = tM.title;
        topicCell.contentLabel.text = tM.content;
        topicCell.timeLabel.text = tM.addtime_f;
        topicCell.commentLabel.text = [tM.comment stringValue];
        return topicCell;

    } else {
        Community_TopicWithImgCell *topicWithImgCell = [tableView dequeueReusableCellWithIdentifier:@"topicWithImg" forIndexPath:indexPath];
        [topicWithImgCell.iconImg sd_setImageWithURL:[NSURL URLWithString:tM.icon]];
        topicWithImgCell.titleLabel.text = tM.title;
        topicWithImgCell.contentLabel.text = tM.content;
        topicWithImgCell.timeLabel.text = tM.addtime_f;
        topicWithImgCell.commentLabel.text = [tM.comment stringValue];
        return topicWithImgCell;

    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 350;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
